3 use Wikimedia\TestingAccessWrapper
;
6 * @group ResourceLoader
8 class ResourceLoaderClientHtmlTest
extends PHPUnit_Framework_TestCase
{
10 protected static function expandVariables( $text ) {
11 return strtr( $text, [
12 '{blankVer}' => ResourceLoaderTestCase
::BLANK_VERSION
16 protected static function makeContext( $extraQuery = [] ) {
17 $conf = new HashConfig( [
18 'ResourceLoaderSources' => [],
19 'ResourceModuleSkinStyles' => [],
20 'ResourceModules' => [],
21 'EnableJavaScriptTest' => false,
22 'ResourceLoaderDebug' => false,
23 'LoadScript' => '/w/load.php',
25 return new ResourceLoaderContext(
26 new ResourceLoader( $conf ),
27 new FauxRequest( array_merge( [
31 'target' => 'phpunit',
36 protected static function makeModule( array $options = [] ) {
37 return new ResourceLoaderTestModule( $options );
40 protected static function makeSampleModules() {
43 'test.top' => [ 'position' => 'top' ],
44 'test.private.top' => [ 'group' => 'private', 'position' => 'top' ],
45 'test.private.bottom' => [ 'group' => 'private', 'position' => 'bottom' ],
46 'test.shouldembed.empty' => [ 'shouldEmbed' => true, 'isKnownEmpty' => true ],
47 'test.shouldembed' => [ 'shouldEmbed' => true ],
49 'test.styles.pure' => [ 'type' => ResourceLoaderModule
::LOAD_STYLES
],
50 'test.styles.mixed' => [],
51 'test.styles.noscript' => [
52 'type' => ResourceLoaderModule
::LOAD_STYLES
,
53 'group' => 'noscript',
55 'test.styles.user' => [
56 'type' => ResourceLoaderModule
::LOAD_STYLES
,
59 'test.styles.user.empty' => [
60 'type' => ResourceLoaderModule
::LOAD_STYLES
,
62 'isKnownEmpty' => true,
64 'test.styles.private' => [
65 'type' => ResourceLoaderModule
::LOAD_STYLES
,
67 'styles' => '.private{}',
69 'test.styles.shouldembed' => [
70 'type' => ResourceLoaderModule
::LOAD_STYLES
,
71 'shouldEmbed' => true,
72 'styles' => '.shouldembed{}',
76 'test.scripts.top' => [ 'position' => 'top' ],
77 'test.scripts.user' => [ 'group' => 'user' ],
78 'test.scripts.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ],
79 'test.scripts.raw' => [ 'isRaw' => true ],
80 'test.scripts.shouldembed' => [ 'shouldEmbed' => true ],
82 'test.ordering.a' => [ 'shouldEmbed' => false ],
83 'test.ordering.b' => [ 'shouldEmbed' => false ],
84 'test.ordering.c' => [ 'shouldEmbed' => true, 'styles' => '.orderingC{}' ],
85 'test.ordering.d' => [ 'shouldEmbed' => true, 'styles' => '.orderingD{}' ],
86 'test.ordering.e' => [ 'shouldEmbed' => false ],
88 return array_map( function ( $options ) {
89 return self
::makeModule( $options );
94 * @covers ResourceLoaderClientHtml::getDocumentAttributes
96 public function testGetDocumentAttributes() {
97 $client = new ResourceLoaderClientHtml( self
::makeContext() );
98 $this->assertInternalType( 'array', $client->getDocumentAttributes() );
102 * @covers ResourceLoaderClientHtml::__construct
103 * @covers ResourceLoaderClientHtml::setModules
104 * @covers ResourceLoaderClientHtml::setModuleStyles
105 * @covers ResourceLoaderClientHtml::setModuleScripts
106 * @covers ResourceLoaderClientHtml::getData
107 * @covers ResourceLoaderClientHtml::getContext
109 public function testGetData() {
110 $context = self
::makeContext();
111 $context->getResourceLoader()->register( self
::makeSampleModules() );
113 $client = new ResourceLoaderClientHtml( $context );
114 $client->setModules( [
116 'test.private.bottom',
119 'test.shouldembed.empty',
123 $client->setModuleStyles( [
125 'test.styles.user.empty',
126 'test.styles.private',
128 'test.styles.shouldembed',
129 'test.unregistered.styles',
131 $client->setModuleScripts( [
133 'test.scripts.user.empty',
135 'test.scripts.shouldembed',
136 'test.unregistered.scripts',
141 'test.private.top' => 'loading',
142 'test.private.bottom' => 'loading',
143 'test.shouldembed.empty' => 'ready',
144 'test.shouldembed' => 'loading',
145 'test.styles.pure' => 'ready',
146 'test.styles.user.empty' => 'ready',
147 'test.styles.private' => 'ready',
148 'test.styles.shouldembed' => 'ready',
149 'test.scripts' => 'loading',
150 'test.scripts.top' => 'loading',
151 'test.scripts.user.empty' => 'ready',
152 'test.scripts.shouldembed' => 'loading',
164 'test.scripts.shouldembed',
167 'styles' => [ 'test.styles.private', 'test.styles.shouldembed' ],
169 'test.private.bottom',
176 $access = TestingAccessWrapper
::newFromObject( $client );
177 $this->assertEquals( $expected, $access->getData() );
181 * @covers ResourceLoaderClientHtml::setConfig
182 * @covers ResourceLoaderClientHtml::setExemptStates
183 * @covers ResourceLoaderClientHtml::getHeadHtml
184 * @covers ResourceLoaderClientHtml::getLoad
185 * @covers ResourceLoader::makeLoaderStateScript
187 public function testGetHeadHtml() {
188 $context = self
::makeContext();
189 $context->getResourceLoader()->register( self
::makeSampleModules() );
191 $client = new ResourceLoaderClientHtml( $context );
192 $client->setConfig( [ 'key' => 'value' ] );
193 $client->setModules( [
197 $client->setModuleStyles( [
199 'test.styles.private',
201 $client->setModuleScripts( [
204 $client->setExemptStates( [
205 'test.exempt' => 'ready',
208 // @codingStandardsIgnoreStart Generic.Files.LineLength
209 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
210 . '<script>(window.RLQ=window.RLQ||[]).push(function(){'
211 . 'mw.config.set({"key":"value"});'
212 . 'mw.loader.state({"test.exempt":"ready","test.private.top":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.scripts.top":"loading"});'
213 . 'mw.loader.implement("test.private.top@{blankVer}",function($,jQuery,require,module){},{"css":[]});'
214 . 'mw.loader.load(["test.top"]);'
215 . 'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts.top\u0026only=scripts\u0026skin=fallback");'
216 . '});</script>' . "\n"
217 . '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>' . "\n"
218 . '<style>.private{}</style>' . "\n"
219 . '<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&skin=fallback"></script>';
220 // @codingStandardsIgnoreEnd
221 $expected = self
::expandVariables( $expected );
223 $this->assertEquals( $expected, $client->getHeadHtml() );
227 * @covers ResourceLoaderClientHtml::getBodyHtml
228 * @covers ResourceLoaderClientHtml::getLoad
230 public function testGetBodyHtml() {
231 $context = self
::makeContext();
232 $context->getResourceLoader()->register( self
::makeSampleModules() );
234 $client = new ResourceLoaderClientHtml( $context );
235 $client->setConfig( [ 'key' => 'value' ] );
236 $client->setModules( [
238 'test.private.bottom',
240 $client->setModuleScripts( [
245 $expected = self
::expandVariables( $expected );
247 $this->assertEquals( $expected, $client->getBodyHtml() );
250 public static function provideMakeLoad() {
252 // @codingStandardsIgnoreStart Generic.Files.LineLength
255 'modules' => [ 'test.unknown' ],
256 'only' => ResourceLoaderModule
::TYPE_STYLES
,
261 'modules' => [ 'test.styles.private' ],
262 'only' => ResourceLoaderModule
::TYPE_STYLES
,
263 'output' => '<style>.private{}</style>',
267 'modules' => [ 'test.private.top' ],
268 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
269 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private.top@{blankVer}",function($,jQuery,require,module){},{"css":[]});});</script>',
273 // Eg. startup module
274 'modules' => [ 'test.scripts.raw' ],
275 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
276 'output' => '<script async="" src="/w/load.php?debug=false&lang=nl&modules=test.scripts.raw&only=scripts&skin=fallback"></script>',
279 'context' => [ 'sync' => true ],
280 'modules' => [ 'test.scripts.raw' ],
281 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
282 'output' => '<script src="/w/load.php?debug=false&lang=nl&modules=test.scripts.raw&only=scripts&skin=fallback&sync=1"></script>',
286 'modules' => [ 'test.scripts.user' ],
287 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
288 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts.user\u0026only=scripts\u0026skin=fallback\u0026user=Example\u0026version=0a56zyi");});</script>',
291 'context' => [ 'debug' => true ],
292 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
293 'only' => ResourceLoaderModule
::TYPE_STYLES
,
294 'output' => '<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.mixed&only=styles&skin=fallback"/>' . "\n"
295 . '<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>',
298 'context' => [ 'debug' => false ],
299 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
300 'only' => ResourceLoaderModule
::TYPE_STYLES
,
301 'output' => '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.mixed%2Cpure&only=styles&skin=fallback"/>',
305 'modules' => [ 'test.styles.noscript' ],
306 'only' => ResourceLoaderModule
::TYPE_STYLES
,
307 'output' => '<noscript><link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.noscript&only=styles&skin=fallback"/></noscript>',
311 'modules' => [ 'test.shouldembed' ],
312 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
313 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.shouldembed@09p30q0",function($,jQuery,require,module){},{"css":[]});});</script>',
317 'modules' => [ 'test.styles.shouldembed' ],
318 'only' => ResourceLoaderModule
::TYPE_STYLES
,
319 'output' => '<style>.shouldembed{}</style>',
323 'modules' => [ 'test.scripts.shouldembed' ],
324 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
325 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.state({"test.scripts.shouldembed":"ready"});});</script>',
329 'modules' => [ 'test', 'test.shouldembed' ],
330 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
331 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test\u0026skin=fallback");mw.loader.implement("test.shouldembed@09p30q0",function($,jQuery,require,module){},{"css":[]});});</script>',
335 'modules' => [ 'test.styles.pure', 'test.styles.shouldembed' ],
336 'only' => ResourceLoaderModule
::TYPE_STYLES
,
338 '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>' . "\n"
339 . '<style>.shouldembed{}</style>'
343 'modules' => [ 'test.ordering.a', 'test.ordering.e', 'test.ordering.b', 'test.ordering.d', 'test.ordering.c' ],
344 'only' => ResourceLoaderModule
::TYPE_STYLES
,
346 '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.ordering.a%2Cb&only=styles&skin=fallback"/>' . "\n"
347 . '<style>.orderingC{}.orderingD{}</style>' . "\n"
348 . '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.ordering.e&only=styles&skin=fallback"/>'
350 // @codingStandardsIgnoreEnd
355 * @dataProvider provideMakeLoad
356 * @covers ResourceLoaderClientHtml::makeLoad
357 * @covers ResourceLoaderClientHtml::makeContext
358 * @covers ResourceLoader::makeModuleResponse
359 * @covers ResourceLoaderModule::getModuleContent
360 * @covers ResourceLoader::getCombinedVersion
361 * @covers ResourceLoader::createLoaderURL
362 * @covers ResourceLoader::createLoaderQuery
363 * @covers ResourceLoader::makeLoaderQuery
364 * @covers ResourceLoader::makeInlineScript
366 public function testMakeLoad( array $extraQuery, array $modules, $type, $expected ) {
367 $context = self
::makeContext( $extraQuery );
368 $context->getResourceLoader()->register( self
::makeSampleModules() );
369 $actual = ResourceLoaderClientHtml
::makeLoad( $context, $modules, $type, $extraQuery );
370 $expected = self
::expandVariables( $expected );
371 $this->assertEquals( $expected, (string)$actual );